package com.vegetable.modules.service.baseData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.baseData.TrainNumRecord;
import com.vegetable.modules.mapper.baseData.TrainNumRecordMapper;
import com.vegetable.modules.service.baseData.ITrainNumRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class TrainNumRecordServiceImpl extends ServiceImpl<TrainNumRecordMapper, TrainNumRecord> implements ITrainNumRecordService {
    @Override
    public Integer queryTrainNum(Long customerId) {
        Integer num = 0;
        QueryWrapper<TrainNumRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customerId);
        TrainNumRecord record = this.baseMapper.selectOne(wrapper);
        if(ObjectUtil.isNotNull(record)){
            num = record.getTrainNum();
        }
        return num;
    }

    @Override
    public void recordTrainNum(Long customerId, Integer trainNum) {
        TrainNumRecord record = new TrainNumRecord();
        record.setTrainNum(trainNum);
        record.setCustomerId(customerId);

        QueryWrapper<TrainNumRecord> wrapper = new QueryWrapper();
        wrapper.eq("customer_id", customerId);
        this.saveOrUpdate(record, wrapper);
    }
}
